package com.loubii.sunshineSchool.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.loubii.sunshineSchool.FinalVirable;
import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.adapter.BaseRecycleAdapter;
import com.loubii.sunshineSchool.adapter.SSCardAdapter;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.pojo.ActivityContainer;
import com.loubii.sunshineSchool.ui.dialog.ListPopWindow;
import com.loubii.sunshineSchool.util.DensityUtil;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

public class FragmentActivity extends BaseEventFragment {
    private static final String ARG_PARAM1 = "param1";
    //    @BindView(R.id.ll_add_deposit)
//    LinearLayout mLlAddDeposit;
    //    @BindView(R.id.ll_add_credit)
//    LinearLayout mLlAddCredit;
    @BindView(R.id.rv_card)
    RecyclerView mRvCard;
    private SSCardAdapter mAdapter;
    private ListPopWindow list1;
    private ListPopWindow list2;
    private ListPopWindow list3;
    private int currentPage;
    @BindView(R.id.tv_select_school)
    TextView school;
    @BindView(R.id.tv_select_type)
    TextView type;
    @BindView(R.id.tv_order)
    TextView order;
    @BindView(R.id.tv_title_time)
    EditText editText;
    @BindView(R.id.tv_remove)
    TextView remove;
    ActivityContainer container;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initView(View view) {
        mRvCard.setVisibility(View.VISIBLE);
        initRecycleView();
        setListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        findActivity();
    }


    private void setListener() {
        mAdapter.setOnItemClickListener(new BaseRecycleAdapter.OnItemClickListener() {

            //三种卡，对应三个页面
            @Override
            public void onItemClick(View view, int position) {
                if (SSCardAdapter.TYPE_END == mAdapter.getItemViewType(position)) { //添加信用卡
                    currentPage++;
                    findActivity();
                }
            }
        });
    }

    private void initRecycleView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRvCard.setLayoutManager(linearLayoutManager);
        mAdapter = new SSCardAdapter(context);
        mRvCard.setAdapter(mAdapter);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_card;
    }

    private void findActivity() {
        String text = editText.getText().toString();
        String schoolName = ((String) school.getText()).equals("学校") ? "" : (String) school.getText();
        Integer typeName = ((String) type.getText()).equals("活动类型") ? -1 : FinalVirable.types.indexOf(type.getText());
        Integer orderName = ((String) order.getText()).equals("排序") ? 0 : FinalVirable.orders.indexOf(order.getText());
        HttpBin.findActivity(context, text, schoolName, typeName, orderName, currentPage, new HttpBin.ResponseListener<ActivityContainer>() {
            @Override
            public void onResponse(Call<ActivityContainer> call, Response<ActivityContainer> response) {
                container = response.body();
                mAdapter.setData(container);
            }

            @Override
            public void onFailure(Call<ActivityContainer> call, Throwable t) {

            }
        });
    }

    @OnClick({R.id.tv_select_school, R.id.tv_select_type, R.id.tv_order, R.id.tv_search, R.id.tv_remove})
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.tv_select_school:
                list1 = new ListPopWindow(context, 4);
                list1.setAnchorView(context.findViewById(R.id.tv_select_school));
                list1.setHorizontalOffset(DensityUtil.dip2px(5));
                list1.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return FinalVirable.schools.size();
                    }

                    @Override
                    public Object getItem(int i) {
                        return null;
                    }

                    @Override
                    public long getItemId(int i) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder;

                        if (convertView == null) {
                            convertView = LayoutInflater.from(context).inflate(R.layout.item_chart_popup, null);
                            holder = new ViewHolder();
                            holder.mTvClassify = (TextView) convertView.findViewById(R.id.tv_classify);
                            convertView.setTag(holder);
                        } else {
                            holder = (ViewHolder) convertView.getTag();
                        }
                        holder.mTvClassify.setText(FinalVirable.schools.get(position));
                        return convertView;
                    }

                    class ViewHolder {
                        TextView mTvClassify;
                    }
                });
                list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        school.setText(FinalVirable.schools.get(position));
                        list1.dismiss();
                        findActivity();
                    }
                });
                list1.show();
                break;
            case R.id.tv_select_type:
                list2 = new ListPopWindow(context, 4);
                list2.setAnchorView(context.findViewById(R.id.tv_select_type));
                list2.setHorizontalOffset(DensityUtil.dip2px(5));
                list2.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return FinalVirable.types.size();
                    }

                    @Override
                    public Object getItem(int i) {
                        return null;
                    }

                    @Override
                    public long getItemId(int i) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder;

                        if (convertView == null) {
                            convertView = LayoutInflater.from(context).inflate(R.layout.item_chart_popup, null);
                            holder = new ViewHolder();
                            holder.mTvClassify = (TextView) convertView.findViewById(R.id.tv_classify);
                            convertView.setTag(holder);
                        } else {
                            holder = (ViewHolder) convertView.getTag();
                        }
                        holder.mTvClassify.setText(FinalVirable.types.get(position));
                        return convertView;
                    }

                    class ViewHolder {
                        TextView mTvClassify;
                    }
                });
                list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //Logger.e("click " + position);
                        type.setText(FinalVirable.types.get(position));
                        list2.dismiss();
                        findActivity();
                    }
                });
                list2.show();
                break;
            case R.id.tv_order:
                list3 = new ListPopWindow(context, 2);
                list3.setAnchorView(context.findViewById(R.id.tv_order));
                list3.setHorizontalOffset(DensityUtil.dip2px(5));
                list3.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return FinalVirable.orders.size();
                    }

                    @Override
                    public Object getItem(int i) {
                        return null;
                    }

                    @Override
                    public long getItemId(int i) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder;

                        if (convertView == null) {
                            convertView = LayoutInflater.from(context).inflate(R.layout.item_chart_popup, null);
                            holder = new ViewHolder();
                            holder.mTvClassify = (TextView) convertView.findViewById(R.id.tv_classify);
                            convertView.setTag(holder);
                        } else {
                            holder = (ViewHolder) convertView.getTag();
                        }
                        holder.mTvClassify.setText(FinalVirable.orders.get(position));
                        return convertView;
                    }

                    class ViewHolder {
                        TextView mTvClassify;
                    }
                });
                list3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //Logger.e("click " + position);
                        order.setText(FinalVirable.orders.get(position));
                        list3.dismiss();
                        findActivity();
                    }
                });
                list3.show();
                break;
            case R.id.tv_search:
                findActivity();
                break;
            case R.id.tv_remove:
                school.setText("学校");
                type.setText("活动类型");
                order.setText("排序");
                editText.setText("");
                currentPage = 1;
                findActivity();
                break;

//            case R.id.ll_add_credit:
//                intent = new Intent(getActivity(), CardAddActivity.class);
//                intent.putExtra(Extra.CARD_TYPE, Extra.CARD_CREDIT);
//                startActivity(intent);
//                break;
        }
    }
}
